<?php

class ThirdController extends CloudControllerBase
{

    const REDIS_KEY_WECHAT_ACCESS_TOKEN = "REDIS_KEY_WECHAT_ACCESS_TOKEN";

    const REDIS_KEY_WECHAT_JSAPI_TICKET = "REDIS_KEY_WECHAT_JSAPI_TICKET";




    private function getWechatToken()
    {
        $redisKey = $this::REDIS_KEY_WECHAT_ACCESS_TOKEN;
        if ($this->redis->exists($redisKey)) {
            $token = $this->redis->get($redisKey);
            return array(
                "success" => true,
                "token" => $token
            );
        } else {

            $appId = $this->wechatDev["appid"];
            $secret =$this->wechatDev["secret"];

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$secret";

            $data = $this->curlRequest($url, 'GET');


            if (!empty($data)) {
                $jsonData = json_decode($data);
                if ($jsonData->errcode!=0) {
                    return array(
                        "success" => false,
                        "msg" => $data
                    );
                } else {

                    $accessToken = $jsonData->access_token;
                    $lifeTime = intval($jsonData->expires_in);
                    $this->redis->setex($redisKey, $lifeTime, $accessToken);
                    return array(
                        "success" => true,
                        "token" => $accessToken
                    );
                }
            } else {
                return array(
                    "success" => false,
                    "msg" => "微信access_token服务器访问错误"
                );
            }
        }
    }

    private function getWechatJsapiTicket()
    {
        $redisKey = $this::REDIS_KEY_WECHAT_JSAPI_TICKET;
        if ($this->redis->exists($redisKey)) {
            $ticket = $this->redis->get($redisKey);
            return array(
                "success" => true,
                "ticket" => $ticket
            );
        } else {
            $result = $this->getWechatToken();
            if ($result["success"]) {
                $token = $result["token"];
                $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
                $data = $this->curlRequest($url, 'GET');
                if (!empty($data)) {
                    $jsonData = json_decode($data);
                    if ($jsonData->errcode!=0) {
                        return array(
                            "success" => false,
                            "msg" => $data
                        );
                    } else {

                        $ticket = $jsonData->ticket;
                        $lifeTime = intval($jsonData->expires_in);
                        $this->redis->setex($redisKey, $lifeTime, $ticket);
                        return array(
                            "success" => true,
                            "ticket" => $ticket
                        );
                    }

                } else {
                    return array(
                        "success" => false,
                        "msg" => "微信ticket服务器访问错误"
                    );
                }
            }
            return $result;

        }
    }

    //获取签名
    public function wechatSignatureAction()
    {
        $result = $this->getWechatJsapiTicket();
        if ($result["success"]) {
            $ticket = $result["ticket"];
            $timeStamp = time();
            $noncestr=$this->guid();
            $server=$_SERVER['HTTP_REFERER'];
            $targetUrl="jsapi_ticket=$ticket&noncestr=$noncestr&timestamp=$timeStamp&url=$server";
            $signature = sha1($targetUrl);
            $this->returnJson(array(
                "appid"=>$this->wechatDev["appid"],
                "signature" => $signature,
                "timestamp"=>$timeStamp,
                "noncestr"=>$noncestr,
                "url"=>$server
            ));
        } else {
            $this->returnError($result["msg"]);
        }

    }


}
